package com.java1412.service.impl;

import com.java1412.dto.CartDto;
import com.java1412.entity.Cart;
import com.java1412.entity.Product;
import com.java1412.mapper.CartMapper;
import com.java1412.mapper.ProductMapper;
import com.java1412.service.CartService;
import com.java1412.utils.ResponseUtil;
import com.java1412.vo.CartVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author java1412
 * @since 2023-06-25 16:10:54
 */
@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Resource
    private CartMapper cartMapper;

    @Resource
    private ProductMapper productMapper;

    @Override
    public ResponseUtil findCount(Integer userId) {
        Integer count = cartMapper.findCount(userId);
        return ResponseUtil.ok("获取购物车数量成功", count);
    }

    @Override
    public ResponseUtil find(Integer userId) {
        List<Cart> cartList = cartMapper.find(userId);
        // 对数据数据合并,获取商品名称和单价
        List<CartDto> cartDtoList = new ArrayList<>();
        for (Cart cart : cartList) {
            // 获取商品编号
            Integer productId = cart.getProductId();
            Product product = productMapper.findById(productId);
            // 创建CartDto对象
            CartDto cartDto = new CartDto();
            // BeanUtils.copyProperties()将第一个参数对象中的同名属性的值赋值到第一个同名属性中
            // 第一个参数表示源对象
            // 第二个参数是目标对象
            BeanUtils.copyProperties(cart, cartDto);
            // 但是有两个属性是cart中没有的，name还有price
            cartDto.setName(product.getName());
            cartDto.setPrice(product.getPrice());
            // 将cartDto加到cartDtoList
            cartDtoList.add(cartDto);
        }
        return ResponseUtil.ok("获取购物车数据成功", cartDtoList);
    }

    // 修改购物车状态
    @Override
    public ResponseUtil updateCartStatus(CartVo cartVo) {
        ResponseUtil r = null;
        Integer result = cartMapper.updateCartStatus(cartVo);
        if (result.intValue() <= 0) {
            r = ResponseUtil.fail("修改购物车失败");
        } else {
            r = ResponseUtil.ok("修改购物车成功");
        }
        return r;
    }

    @Override
    public ResponseUtil deleteById(Integer cartId) {
        ResponseUtil r = null;
        Integer result = cartMapper.deleteById(cartId);
        if (result.intValue() <= 0) {
            r = ResponseUtil.fail("删除购物车失败");
        } else {
            r = ResponseUtil.ok("删除购物车成功");
        }
        return r;
    }

    @Override
    public ResponseUtil deleteCartsByIds(Integer[] ids) {
        ResponseUtil r = null;
        Integer result = cartMapper.deleteCartsByIds(ids);
        if (result.intValue() <= 0) {
            r = ResponseUtil.fail("批量删除购物车失败");
        } else {
            r = ResponseUtil.ok("批量删除购物车成功");
        }
        return r;
    }

    @Override
    public ResponseUtil deleteCartsByUserId(Integer userId) {
        cartMapper.deleteCartsByUserId(userId);
        return ResponseUtil.ok("删除已经购买的购物车信息成功");
    }
}
